package com.hx.thread.countdownlaunch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Minghua Chen
 * @date 2020-09-05 23:11
 */
public class RunDemo2 {
    private static final Logger logger = LoggerFactory.getLogger(RunDemo2.class);

    public static void main(String[] args) throws InterruptedException {
        logger.info("运动员有5s的等待时间:");
        CountDownLatch countDownLatch = new CountDownLatch(1);
        ExecutorService service = Executors.newFixedThreadPool(5);
        for (int i = 1; i < 6; i++) {
            int finalI = i;
            int finalI1 = i;
            Runnable task = () -> {
                logger.info(finalI + "号运动员准备完毕,等待发令枪.");
                try {
                    // 每个线程到这里进行等待
                    countDownLatch.await();
                    logger.info(finalI1 + "号运动员开始跑.");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            };
            // 提交任务到线程池
            service.submit(task);
        }
        Thread.sleep(5000);
        logger.info("5s时间已经到了,开始法令:蹦,比赛开始!");
        countDownLatch.countDown();
        service.shutdown();
    }
}
